package com.llkj.contractmanagement.customview.photoview;

/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.Constant;
import com.llkj.contractmanagement.utils.ImageLoaderUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * 看多张图片，放大缩小点击关闭功能
 *
 * @author Administrator
 */

/**
 * Lock/Unlock button is added to the ActionBar. Use it to temporarily disable
 * ViewPager navigation in order to correctly interact with ImageView by
 * gestures. Lock/Unlock state of ViewPager is saved and restored on
 * configuration changes.
 * <p/>
 * Julia Zudikova
 */

public class ViewPagerActivity extends BaseActivity implements View.OnClickListener {

    private static final String ISLOCKED_ARG = "isLocked";

    private ViewPager mViewPager;
    private ArrayList<String> urls;
    private Intent bigIntent;
    @Bind(R.id.iv_left_add)
    ImageView iv_left;
    @Bind(R.id.iv_right_add)
    ImageView iv_right;

    private int position;
    @Bind(R.id.title_look_pic)
    TitleBar titleBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        if (savedInstanceState != null) {
            boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
            ((HackyViewPager) mViewPager).setLocked(isLocked);
        }
        setListenter();
    }

    private void init() {
        bigIntent = getIntent();
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        if (bigIntent.hasExtra(Constant.LIST)) {
            urls = (ArrayList<String>) bigIntent.getSerializableExtra(Constant.LIST);
        }
        if (bigIntent.hasExtra("position")) {
            position = bigIntent.getIntExtra("position", 0);

        }
        mViewPager.setAdapter(new SamplePagerAdapter());
        mViewPager.setCurrentItem(position);
    }

    private void setListenter() {
        titleBar.setTopBarClickListener(this);
        iv_left.setOnClickListener(this);
        iv_right.setOnClickListener(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_view_pager;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left_add:
                position--;
                if (position < 0) {
                    ToastUtil.makeShortText(this, "已是第一张图片");
                    position = 0;
                    return;
                }
                mViewPager.setCurrentItem(position);
                break;
            case R.id.iv_right_add:
                position++;
                if (position > urls.size()-1) {
                    ToastUtil.makeShortText(this, "已是最后一张图片");
                    position = urls.size()-1;
                    return;
                }
                mViewPager.setCurrentItem(position);
                break;
        }
    }

    class SamplePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
//            ImageLoader.getInstance().displayImage(
//                    UrlConfig.ROOT_IMG_URL + urls.get(position), photoView,
//                    PhotographyApplication.options);
            //假数据使用
//                    urls.get(position), photoView,
//                    MyApplication.options);
            ImageLoaderUtil.displayImage(urls.get(position), photoView);

            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {

                @Override
                public void onPhotoTap(View view, float x, float y) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    private boolean isViewPagerActive() {
        return (mViewPager != null && mViewPager instanceof HackyViewPager);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (isViewPagerActive()) {
            outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) mViewPager).isLocked());
        }
        super.onSaveInstanceState(outState);
    }

}
